package com.ship.dispatch.controller.orderPay;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.ship.common.core.base.Page;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.orderPay.SpFundManage;
import com.ship.dispatch.bean.orderPay.SpFundManageDetail;
import com.ship.dispatch.bo.orderPay.SpFundManageDetailBo;
import com.ship.dispatch.bo.orderPay.SpFundManageUpdateBo;
import com.ship.dispatch.service.ExportService;
import com.ship.dispatch.service.orderPay.SpFundManageDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

/**
 * 资金管理明细 前端控制器
 *
 * @author zhangyh
 * @since 2024-01-10
 */
@RestController
@RequestMapping("/fundManage/detail/")
@Api(value = "spFundManageDetail", tags = "资金管理明细前端控制器")
public class SpFundManageDetailController {

    @Autowired
    private SpFundManageDetailService spFundManageDetailService;

    @Autowired
    private ExportService exportService;

    /**
     * 资金管理分页
     *
     * @param fundManageDetail
     * @return
     */
    @ApiOperation(value = "资金管理分页")
    @PostMapping("fundManageDetailPage")
    public JsonResult<PageInfo<SpFundManageDetail>> page(@RequestBody SpFundManageDetail fundManageDetail) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("corporate_name");
        SpFundManageDetailBo detailBo = new SpFundManageDetailBo();
        BeanUtils.copyProperties(fundManageDetail,detailBo);
        PageInfo<SpFundManageDetail> pageInfo = spFundManageDetailService.getManageDetailPage(detailBo);
//        Page<SpFundManageDetail> page = new Page<>();
//        page.setPageSize(detailIPage.getSize());
//        page.setPageNum(detailIPage.getCurrent());
//        page.setTotal(detailIPage.getTotal());
//        page.setList(detailIPage.getRecords());
//        Page<SpFundManageDetail> page = spFundManageDetailService.page(fundManageDetail,queryWrapper);
        List<SpFundManageDetail> details = pageInfo.getList();
//        if(details.size() == 0){
//            return JsonResult.success(page);
//        }
//        details = details.stream()
//                .collect(Collectors.toMap(SpFundManageDetail::getCorporateName, detail -> detail, (oldValue, newValue) -> oldValue))
//                .values()
//                .stream()
//                .collect(Collectors.toList());
//
        for (SpFundManageDetail detail : details) {
            detail.setFundOrderNumber(spFundManageDetailService.getOrderCountByCorporateName(detail.getCorporateName()));
        }
//
//        page.setList(details);
//        page.setTotal(details.size());
        return JsonResult.success(pageInfo);
    }

    /**
     * 资金管理分页导出
     *
     * @return
     */
    @ApiOperation(value = "资金管理分页导出")
    @PostMapping("fundManageDetailPage/export")
    @Log(title = "资金管理分页导出",businessType = BusinessType.EXPORT)
    public JsonResult fundManageDetailPageExport(@RequestBody SpFundManageDetail fundManageDetail) {
//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        if(fundManageDetail.getIds() != null && fundManageDetail.getIds().size() > 0){
//            queryWrapper.in("id",fundManageDetail.getIds());
//        }
//        queryWrapper.groupBy("corporate_name");
        SpFundManageDetailBo detailBo = new SpFundManageDetailBo();
        BeanUtils.copyProperties(fundManageDetail,detailBo);
//        IPage<SpFundManageDetail> detailIPage = spFundManageDetailService.getExportManageDetail(detailBo);
//        Page<SpFundManageDetail> page = new Page<>();
//        page.setPageSize(999999999);
//        page.setPageNum(1);
//        page.setTotal(detailIPage.getTotal());
//        page.setList(detailIPage.getRecords());
        List<SpFundManageDetail> exportList = spFundManageDetailService.getExportManageDetail(detailBo);
//        if(exportList.size() > 0){
//            exportList = exportList.stream()
//                    .collect(Collectors.toMap(SpFundManageDetail::getCorporateName, detail -> detail, (oldValue, newValue) -> oldValue))
//                    .values()
//                    .stream()
//                    .collect(Collectors.toList());
//        }
        for (SpFundManageDetail spFundManageDetail : exportList) {
            spFundManageDetail.setAccountBalanceStr(spFundManageDetail.getAccountBalanceStr());
            spFundManageDetail.setCorporateTypeStr(spFundManageDetail.getCorporateTypeStr());
            spFundManageDetail.setTradeBodyStr(spFundManageDetail.getTradeBodyStr());
            if(StringUtils.isNotBlank(spFundManageDetail.getCorporateName())){
                spFundManageDetail.setFundOrderNumber(spFundManageDetailService.getOrderCountByCorporateName(spFundManageDetail.getCorporateName()));
            }else{
                spFundManageDetail.setFundOrderNumber(0);
            }
        }

        Field[] fields = SpFundManageDetail.class.getDeclaredFields();
        String url = exportService.pageExport(exportList,fields,exportService.listStrByModuleAndUser
                ("sp_fund_manage_detail", SecurityUtils.getUserId()), "资金管理信息");
        return JsonResult.success(url);
    }

    /**
     * 统计支付次数
     *
     * @return
     */
    @ApiOperation(value = "统计支付次数")
    @GetMapping("getPayNumberTotal")
    public JsonResult getPayNumberTotal() {
        Integer total = spFundManageDetailService.getPayNumberTotal();
        return JsonResult.success(total);
    }

    /**
     * 统计账户余额
     *
     * @return
     */
    @ApiOperation(value = "统计账户余额")
    @GetMapping("getAccountBalanceTotal")
    public JsonResult getAccountBalanceTotal() {
        BigDecimal total = spFundManageDetailService.getAccountBalanceTotal();
        return JsonResult.success(total);
    }

    /**
     * 统计船舶代理平均费用
     *
     * @return
     */
    @ApiOperation(value = "统计船舶代理平均费用")
    @GetMapping("getShipAgencyAverageBalance")
    public JsonResult getShipAgencyAverageBalance() {
        BigDecimal total = spFundManageDetailService.getShipAgencyAverageBalance();
        return JsonResult.success(total);
    }

    /**
     * 资金明细基础数据统计
     *
     * @return
     */
    @ApiOperation(value = "资金明细基础数据统计")
    @GetMapping("base/statistics")
    public JsonResult<SpFundManageDetail> baseStatistics(@RequestParam(value = "corporateName") String corporateName) {
        return spFundManageDetailService.baseStatistics(corporateName);
    }

    /**
     * 资金明细分页查询
     *
     * @return
     */
    @ApiOperation(value = "资金明细分页查询")
    @PostMapping("page")
    public JsonResult<SpFundManage> getPage(@RequestBody SpFundManage dto) {
        return spFundManageDetailService.getPage(dto);
    }

    /**
     * 资金明细分页导出
     *
     * @return
     */
    @ApiOperation(value = "资金明细分页导出")
    @PostMapping("detail/export")
    @Log(title = "资金明细分页导出",businessType = BusinessType.EXPORT)
    public JsonResult detailPageExport(@RequestBody SpFundManage dto) {
        List<SpFundManage> exportList = spFundManageDetailService.getList(dto);
        for (SpFundManage fundManage : exportList) {
            if (null != fundManage.getOrderStatus()){
                if (1 == fundManage.getOrderStatus()){
                    fundManage.setOrderStatusStr("未审核");
                }
                if (2 == fundManage.getOrderStatus()){
                    fundManage.setOrderStatusStr("已审核");
                }
                if (3 == fundManage.getOrderStatus()){
                    fundManage.setOrderStatusStr("已付款");
                }
            }
            fundManage.setAccountBalanceStr(fundManage.getAccountBalanceStr());
            fundManage.setOrderTypeStr(fundManage.getOrderTypeStr());
            fundManage.setPaidAmountStr(fundManage.getPaidAmountStr());
            fundManage.setPayAmountStr(fundManage.getPayAmountStr());
            fundManage.setPayAmountUnitStr(fundManage.getPayAmountUnitStr());
            fundManage.setAccountInitialBalanceStr(fundManage.getAccountInitialBalanceStr());
            fundManage.setPayEndTimeStr(fundManage.getPayEndTimeStr());
        }
        Field[] fields = SpFundManage.class.getDeclaredFields();
        String url = exportService.pageExport(exportList,fields,exportService.listStrByModuleAndUser
                ("sp_fund_manage",SecurityUtils.getUserId()), "资金明细信息");
        return JsonResult.success(url);
    }

    /**
     * 资金明细状态批量更新
     *
     * @return
     */
    @ApiOperation(value = "资金明细状态批量更新")
    @PostMapping("batch/update")
    @Log(title = "资金明细状态批量更新",businessType = BusinessType.UPDATE)
    public JsonResult batchUpdate(@RequestBody SpFundManageUpdateBo dto) {
        return spFundManageDetailService.batchUpdate(dto);
    }

    /**
     * 账户余额变更
     *
     * @return
     */
    @ApiOperation(value = "账户余额变更")
    @PostMapping("balance/update")
    @Log(title = "账户余额变更",businessType = BusinessType.UPDATE)
    public JsonResult balanceUpdate(@RequestBody SpFundManageUpdateBo dto) {
        return spFundManageDetailService.balanceUpdate(dto);
    }

}
